package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.SystemDictionary;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.qo.SystemDictionaryItemQueryObject;
import cn.wolfcode.crm.service.ISystemDictionaryItemService;
import cn.wolfcode.crm.service.ISystemDictionaryService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private ISystemDictionaryService systemDictionaryService;


    // 字典明细新增或修改
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value={"systemDictionaryItem:saveOrUpdate","字典明细新增或修改"},logical= Logical.OR)
    public JsonResult saveOrUpdate(SystemDictionaryItem systemDictionaryItem){
        try{
            if(systemDictionaryItem.getId() != null){
                systemDictionaryItemService.update(systemDictionaryItem);
            }else {
                systemDictionaryItemService.save(systemDictionaryItem);
            }
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }
        return new JsonResult(true,"操作成功!!!"); // localhost/systemDictionaryItem/list.do
    }

    // 查询所有字典明细数据
    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value={"systemDictionaryItem:delete","字典明细删除"},logical= Logical.OR)
    public JsonResult delete(Long id){
        try{
            systemDictionaryItemService.delete(id);
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult(false,"删除失败");
        }
        return new JsonResult(true,"删除成功"); // localhost/systemDictionaryItem/list.do
    }

    // 查询所有字典明细数据
    @RequestMapping("/list")
    @RequiresPermissions(value={"systemDictionaryItem:list","字典明细列表"},logical= Logical.OR)
    public String list(Model model, @ModelAttribute("qo")SystemDictionaryItemQueryObject qo){
        List<SystemDictionary> systemDictionaries = systemDictionaryService.list();
        model.addAttribute("systemDictionaries",systemDictionaries);
        PageInfo query = systemDictionaryItemService.query(qo);
        model.addAttribute("pageResult", query);
        return "systemDictionaryItem/list";
    }
}
